//
//  ViewController.swift
//  TabBar
//
//  Created by DuyAnh on 8/13/15.
//  Copyright (c) 2015 DuyAnh. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func didChangeValue(vc: UIViewController) {
        var newController = vc
        let oldController = childViewControllers.last as! UIViewController
        
        oldController.willMoveToParentViewController(nil)
        addChildViewController(newController)
        newController.view.frame = oldController.view.frame
        
        transitionFromViewController(oldController, toViewController: newController, duration: 0.25, options: .TransitionCrossDissolve, animations:{ () -> Void in
            // nothing needed here
            }, completion: { (finished) -> Void in
                oldController.removeFromParentViewController()
                newController.didMoveToParentViewController(self)
        })
    }
    
    func getViewController(aClass: AnyClass!) -> UIViewController {
        let storyBoad = UIStoryboard(name: "Main", bundle: nil)
        let identifier = NSStringFromClass(aClass).componentsSeparatedByString(".").last
        return storyBoad.instantiateViewControllerWithIdentifier(identifier!) as! UIViewController
    }
    
    func pushVC(calss: AnyClass) {
        
    }
    
    @IBAction func click_1(sender: AnyObject) {
        didChangeValue(getViewController(View_1))
    }
    
    @IBAction func click_2(sender: AnyObject) {
        didChangeValue(getViewController(View_2))
    }
    
    @IBAction func click_3(sender: AnyObject) {
        didChangeValue(getViewController(View_3))
    }
    
    @IBAction func click_4(sender: AnyObject) {
        didChangeValue(getViewController(View_4))
    }
    
    @IBAction func click_5(sender: AnyObject) {
        didChangeValue(getViewController(View_5))
    }
}

class View_1: UIViewController {}

class View_2: UIViewController {}

class View_3: UIViewController {}

class View_4: UIViewController {}

class View_5: UIViewController {}

